package cn.butcher21.controller;

import cn.butcher21.dao.DaoUser;
import cn.butcher21.dao.DaoWorkImpl.DaoUserImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

@WebServlet("/initData")
public class InitServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String useid = (String) request.getSession().getAttribute("userid");
        DaoUser daoUser = new DaoUserImpl();
        try {
            Map<String, Object> groupMsg = daoUser.getGroupMsg(useid);
            Gson gson = new Gson();
            String json = gson.toJson(groupMsg);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(json);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
